﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using GsnHelper;
using PetaPoco;

namespace ZhongheCms.Models
{
    [MetadataType(typeof(UserMetaData))]
    public partial class GswTblUser
    {
        [ResultColumn]
        public string Password { get; set; }
        [ResultColumn]
        public string PasswordConfirm { get; set; }
        [ResultColumn]
        public List<GswTblModuleText> ModuleTexts { get; set; }
        [ResultColumn]
        public List<GswTblUserPower> UserPowers { get; set; }
        [ResultColumn]
        public string ModuleName { get; set; }
        [ResultColumn]
        public string FuncName { get; set; }
         [ResultColumn]
        public string ActionParam { get; set; }

        public static GswTblUser CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session["user"] == null)
                    return null;
                return HttpContext.Current.Session["user"] as GswTblUser;
            }
        }



        public static string ValidateUser(string username, string password)
        {
            string userLogin;
            var user = FirstOrDefault("where username=@0", username);
            if (user == null)
                userLogin = "无此用户名";
            else
            {
                string sh1Pwd = EncryptPwd.EncodePassword(password, user.PwdSalt);
                if (user.PwdHash.Trim() == sh1Pwd)
                {
                     var userModule = user.UserType.Trim() == "超级用户" ? GswTblModuleText.GetModuleList() : GswTblModuleText.GetModuleList(user.Id);
                    user.ModuleTexts = userModule;
                    HttpContext.Current.Session["user"] = user;
                   
              
                    userLogin = "ok";
                }
                else
                {
                    userLogin = "密码错误";
                }
            }
            return userLogin;
        }
        public static void LoginOut()
        {
            HttpContext.Current.Session["user"] = null;
        //    HttpContext.Current.Session["usermodule"] = null;
            FormsAuthentication.SignOut();
        }

        public static List<GswTblUser> GetCusUserList(int cusId)
        {
            var strSql = "select * from GswTblUsers where AdminFlag=0 and CustomerId=" + cusId;
            strSql += " order by id";
            return Fetch(strSql);
        }

    }
    public class UserMetaData
    {
        [Required(ErrorMessage = "用户名不能为空")]
       
        public string UserName { get; set; }
        [Required(ErrorMessage = "真实姓名不能为空")]
        [StringLength(2, ErrorMessage = "用户名不能为空")]
        public string TrueName { get; set; }
        [Required(ErrorMessage = "密码不允许为空")]
        public string Password { get; set; }
        //[Compare("Password", ErrorMessage = "两次输入不一致")]
        //public string PasswordConfirm { get; set; }

    }
}